/**
 *
 */
package com.guo.hui.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.guo.hui.model.NoteBook;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

/**
 * @author zhengguohui
 * @className MongodbDemoController
 * @description
 * @date 2018年7月6日
 */
@RestController
@RequestMapping(value = "/mongo")
public class MongodbDemoController {

	@Autowired
	private MongoTemplate mongoTemplate;

	@RequestMapping(value = "/add")
	public void mongodAdd() {

		Query query = new Query();
		query.addCriteria(Criteria.where("username").is("machao"));

		List<NoteBook> users = mongoTemplate.find(query, NoteBook.class);
		System.out.println(users);

	}

	@RequestMapping(value = "/file", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String file(@RequestBody String param) {
		JSONObject param_json = JSONObject.parseObject(param);
		String path = param_json.getString("path");
		// String path = "D:/ceshiwenjian";
		String content = "";
		File fileList = new File(path);
		MongoCollection<Document> mongo = mongodb("47.104.139.62:27017", "demo", "FileToMongo");
		if (fileList.exists()) {
			File[] files = fileList.listFiles();
			for (File file : files) {
				content = readFile(file);
				Document doc = Document.parse(content);
				mongo.insertOne(doc);
			}
		} else {
			System.out.println("文件不存在!");
		}
		return JSONObject.toJSONString("ok");
	}

	private String readFile(File file) {
		StringBuilder localStrBulider = new StringBuilder();
		if (file.isFile() && file.exists()) {
			try {
				InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), "UTF-8");
				BufferedReader bufferReader = new BufferedReader(inputStreamReader);
				String lineStr = null;
				try {
					while ((lineStr = bufferReader.readLine()) != null) {
						localStrBulider.append(lineStr);
					}
					bufferReader.close();
					inputStreamReader.close();
				} catch (IOException e) {
					bufferReader.close();
					inputStreamReader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return localStrBulider.toString();

	}

	private MongoCollection<Document> mongodb(String address, String dataname, String coll) {
		MongoClient mongoClient = new MongoClient(address);
		MongoDatabase db = mongoClient.getDatabase(dataname);
		MongoCollection<Document> mongo = db.getCollection(coll);
		return mongo;
	}

}
